<script setup lang="ts">
import { computed, ref, watch } from 'vue'

// 定义水果对象的类型
interface Fruit {
  id: number
  icon: string
  isChecked: boolean
  num: number
  price: number
}

// 存取数据
// 1. 定义常量键
const ListKey = 'cartList'
// 2. 获取localStorage中存储的数据
const res = localStorage.getItem(ListKey)
let list: Fruit[] = []
// 3. 判断 localStorage 中是否存有数据
// 如果为空， 设置为初始值
if (res === null) {
  list = [
    {
      id: 1,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/ll.png',
      isChecked: true,
      num: 2,
      price: 6,
    },
    {
      id: 2,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/smt.png',
      isChecked: false,
      num: 7,
      price: 20,
    },
    {
      id: 3,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/tg.png',
      isChecked: false,
      num: 3,
      price: 40,
    },
    {
      id: 4,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/xg.png',
      isChecked: false,
      num: 10,
      price: 3,
    },
    {
      id: 5,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/yl.png',
      isChecked: false,
      num: 20,
      price: 34,
    },
  ]
} else {
  // 有数据，就赋值
  list = JSON.parse(res)
}

// 水果列表
const fruitList = ref<Fruit[]>(list)

// 4. 监听数组变化， 同步数据到 localStorage 中
watch(
  fruitList,
  () => {
    // 数组 -> JSON
    localStorage.setItem(ListKey, JSON.stringify(fruitList.value))
  },
  { deep: true },
)
// 5. 点击空空如也， 重置数据
const resetList = () => {
  fruitList.value = [
    {
      id: 1,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/ll.png',
      isChecked: true,
      num: 2,
      price: 6,
    },
    {
      id: 2,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/smt.png',
      isChecked: false,
      num: 7,
      price: 20,
    },
    {
      id: 3,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/tg.png',
      isChecked: false,
      num: 3,
      price: 40,
    },
    {
      id: 4,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/xg.png',
      isChecked: false,
      num: 10,
      price: 3,
    },
    {
      id: 5,
      icon: 'https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/yl.png',
      isChecked: false,
      num: 20,
      price: 34,
    },
  ]
}
// ===================================
// 删除方法
const remove = (id: number) => {
  const index = fruitList.value.findIndex((item) => item.id == id)
  fruitList.value.splice(index, 1)
}
// 点击减号方法
const decreaseCount = (item: Fruit) => {
  if (item.num > 1) {
    item.num--
  } else {
    alert('不能再减了~')
  }
}
// 点击加号方法
const addCount = (item: Fruit) => {
  if (item.num < 10) {
    item.num++
  } else {
    alert('不能再加了~')
  }
}
// 全选 计算属性方法
// 是否全部选中需要根据每一项的选中状态计算而来
const isCheckedAll = computed({
  // 获取每一项的值， 判断是否都被选中
  get() {
    return fruitList.value.every((item) => item.isChecked)
  },
  // 赋值
  // v-model = "isCheckedAll" 绑定给了 checkbox
  // 点击全选 时 回传数据给计算属性
  set(value) {
    fruitList.value.forEach((item) => (item.isChecked = value))
  },
})

// 总数量计算方法
const totalCount = computed(() => {
  return fruitList.value
    .filter((item) => item.isChecked)
    .reduce((pre, item) => {
      return pre + item.num
    }, 0)
})

// 计算总价格
const totalPrice = computed(() => {
  return fruitList.value
    .filter((item) => item.isChecked)
    .reduce((pre, item) => {
      return pre + item.num * item.price
    }, 0)
})
</script>

<template>
  <div class="app-container" id="app">
    <!-- 顶部banner -->
    <div class="banner-box">
      <img src="https://hongm-1312445828.cos.ap-nanjing.myqcloud.com/sg_top.png" alt="" />
    </div>
    <!-- 面包屑 -->
    <div class="breadcrumb">
      <span>🏠</span>
      /
      <span>购物车</span>
    </div>
    <!-- 购物车主体 -->
    <div class="main" v-if="fruitList.length > 0">
      <div class="table">
        <!-- 头部 -->
        <div class="thead">
          <div class="tr">
            <div class="th">选中</div>
            <div class="th th-pic">图片</div>
            <div class="th">单价</div>
            <div class="th num-th">个数</div>
            <div class="th">小计</div>
            <div class="th">操作</div>
          </div>
        </div>
        <!-- 身体 -->
        <div class="tbody">
          <div v-for="item in fruitList" :key="item.id" class="tr">
            <div class="td"><input type="checkbox" v-model="item.isChecked" /></div>
            <div class="td"><img :src="item.icon" alt="" /></div>
            <div class="td">{{ item.price }}</div>
            <div class="td">
              <div class="my-input-number">
                <button :disabled="item.num <= 1" class="decrease" @click="decreaseCount(item)">
                  -
                </button>
                <span class="my-input__inner">{{ item.num }}</span>
                <button class="increase" @click="addCount(item)">+</button>
              </div>
            </div>
            <div class="td">{{ item.num * item.price }}</div>
            <div class="td"><button @click="remove(item.id)">删除</button></div>
          </div>
        </div>
      </div>
      <!-- 底部 -->
      <div class="bottom">
        <!-- 全选 -->
        <label class="check-all">
          <input type="checkbox" v-model="isCheckedAll" />
          全选
        </label>
        <div class="right-box">
          <!-- 所有商品总价 -->
          <span class="price-box"
            >总价&nbsp;&nbsp;:&nbsp;&nbsp;¥&nbsp;<span class="price">{{ totalPrice }}</span></span
          >
          <!-- 结算按钮 -->
          <button class="pay">结算( {{ totalCount }} )</button>
        </div>
      </div>
    </div>
    <!-- 空车 -->
    <div class="empty" v-else @click="resetList">🛒空空如也</div>
  </div>
</template>

<style scoped>
@import './style/inputnumber.css';
@import './style/index.css';
</style>
